package com.littcore.saap.assistant.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.littcore.exception.BusiException;
import com.littcore.saap.assistant.dao.UserPortletDao;
import com.littcore.saap.assistant.po.UserPortlet;
import com.littcore.saap.assistant.service.IUserPortletService;
import com.littcore.saap.assistant.vo.UserPortletVo;
import com.littcore.saap.core.module.portal.PortletMeta;
import com.littcore.saap.core.module.portal.PortletMetaManager;
import com.littcore.util.JsonUtils;
import com.littcore.util.StringUtils;

/**
 * .
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年10月21日
 * @version 1.0
 */
public class UserPortletServiceImpl implements IUserPortletService {
  @Resource
  private UserPortletDao userPortletDao;
  
  /**
   * Save.
   *
   * @param portletUid the portlet uid
   * @param userId the user id
   * @param name the name
   * @param refreshInterval the refresh interval
   * @param positionX the position x
   * @param positionY the position y
   * @param params the params
   */
  @Override
  public void save(String portletUid, int userId, String name, int refreshInterval, int positionX, int positionY, Map<String, Object> params)
  {
    UserPortlet userPortlet = new UserPortlet();
    userPortlet.setPortletUid(portletUid);
    userPortlet.setUserId(userId);
    userPortlet.setName(name);
    userPortlet.setTheme("");
    userPortlet.setRefreshInterval(0);    
    //根据现有数据动态计算位置，加到最后面
    positionX = userPortletDao.getMaxPositionX(userId);
    positionY = userPortletDao.getMaxPositionY(userId);
    if(positionY==2)
    {
      positionX++;
      positionY = 0;
    }else {
      positionY++;
    }
    userPortlet.setPositionX(positionX);
    userPortlet.setPositionY(positionY);
    
    if(params!=null && !params.isEmpty())
    {
      try
      {
        String json = JsonUtils.toJSON(params);
        userPortlet.setParams(json);
      } catch (IOException e)
      {
        throw new BusiException("Can't convert params to json.", e);
      }
    }
    userPortlet.setCreateDatetime(new Date());
    userPortletDao.save(userPortlet);    
  }
  
    /**
   * Update.
   * @param userPortlet UserPortlet
   */
  @Override
  public void update(UserPortlet userPortlet)
  {
    userPortletDao.update(userPortlet);
  }     
  
  /* (non-Javadoc)
   * @see com.littcore.saap.assistant.service.IUserPortletService#updateName(java.lang.Integer, java.lang.String)
   */
  public void updateName(Integer id, String name)
  {
    UserPortlet userPortlet = this.load(id);
    userPortlet.setName(name);
    userPortletDao.update(userPortlet);
  }
  
  @Override
  public void delete(Integer id)
  {
    UserPortlet userPortlet = this.load(id);
    this.delete(userPortlet);   
  }
  
  /**
   * Delete by instance.
   * @param id id
   */
  @Override
  public void delete(UserPortlet userPortlet)
  {
    userPortletDao.delete(userPortlet);    
  }
  
  @Override
  public UserPortlet load(Integer id)
  {
    return userPortletDao.load(id);
  }
  
  @Override
  public List<UserPortlet> listByUser(int userId) {
    return userPortletDao.listByUser(userId);
  }
  
  public List<UserPortletVo> findByUser(int userId) {
    List<UserPortlet> userPortlets = this.listByUser(userId);
    List<UserPortletVo> voList = new ArrayList<UserPortletVo>();
    for (UserPortlet userPortlet : userPortlets)
    {
      UserPortletVo vo = new UserPortletVo();
      vo.setId(userPortlet.getId());
      vo.setPortletUid(userPortlet.getPortletUid());
      vo.setName(userPortlet.getName());
      PortletMeta portletMeta = PortletMetaManager.getInstance().getPortletMeta(userPortlet.getPortletUid());    
      vo.setDescr(portletMeta.getDescr());
      if(!StringUtils.isEmpty(userPortlet.getParams()))
      {
        try
        {
          vo.setParams(JsonUtils.toObject(userPortlet.getParams(), Map.class));
        } catch (IOException e)
        {
          throw new BusiException("Can't convert portlet params",e);
        }
      }
      vo.setConfigurable(false);
      vo.setTheme(userPortlet.getTheme());
      vo.setRefreshInterval(userPortlet.getRefreshInterval());
      voList.add(vo);
    }
    return voList;
  }
}
